package com.jshx.sz.gdjt.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * 数据库操作类
 * @author caxiang
 * @time 2013-08-30
 */
public class DataBaseHelper extends SQLiteOpenHelper{
	//数据库名称
	private static final String DB_NAME = "mydata.db"; //数据库名称
	
	//数据库版本号
    private static final int version = 1; //数据库版本
    
    //数据库操纵对象实例
    private static DataBaseHelper dataBaseHelper;
	
    //页面标签
    private final static String TAG = "DataBaseHelper";
    
    private DataBaseHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, DB_NAME, factory, version);
		dataBaseHelper = this;
	}
	
	private DataBaseHelper(Context context) {
        super(context, DB_NAME, null, version);
        dataBaseHelper = this;
    }
	
	/**
	 * 获取数据库操纵对象实例
	 * @param context
	 * @return
	 */
	public static DataBaseHelper getInstance(Context context)
	{
		if(dataBaseHelper == null)
		{
			dataBaseHelper = new DataBaseHelper(context);
		}
		return dataBaseHelper;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.d(TAG, "DataBaseHelper onCreate");
		//创建任务列表表单
		String sql = "create table if not exists task_list(order_num text primary key,status text,check_person text,"
				+ "audit_person text,device_num text, device_name text, work_num text,start_time text, end_time text,"
				+ "work_status text, station_name text, team_num text, dept_num text, process_orderNum text, work_type"
				+ " text, check_type text, check_invidate text, check_level text);";  
		Log.d(TAG, sql);
        db.execSQL(sql);
        //创建任务详情表单1
        sql = "create table if not exists task_detail_type1(order_num text primary key,type text,device_num text,"
				+ "device_type text,version_num text, product_factory text, work_date text,rated_current text, rated_voltage text,"
				+ "install_address text, zddl1 text, zdsx1 text, dzdl1 text, dzsx1 text, zddl2"
				+ " text, zdsx2 text, dzdl2 text, dzsx2 text, qddlz1 text, xl1_1 text, xl1_2 text, lxdgd1 text, qddlz2 text" +
				", xl2_1 text, xl2_2 text, lxdgd2 text, cdvalue1 text, cdimg1 text, cdvalue2 text, cdimg2 text, cdvalue3 text, cdimg3 text" +
				", cdvalue4 text, cdimg4 text, cdvalue5 text, cdimg5 text, remark text, resualt text, hjr_name text, hjr_id text);";  
        Log.d(TAG, sql);
        db.execSQL(sql);
        //创建任务详情表单2
        sql = "create table if not exists task_detail_type2(order_num text primary key,type text,install_address text,"
				+ "run_num text,temperature text, humidity text, resualt text,hjr_name text, hjr_id text,"
				+ "resualt1 text, remark1 text, imgs1 text, resualt2 text, remark2 text, imgs2 text, resualt3 text, remark3 text, imgs3 text" +
				", resualt4 text, remark4 text, imgs4 text, resualt5 text, remark5 text, imgs5 text, resualt6 text, remark6 text, imgs6 text" +
				", resualt7 text, remark7 text, imgs7 text, resualt8 text, remark8 text, imgs8 text, resualt9 text, remark9 text, imgs9 text" +
				", resualt10 text, remark10 text, imgs10 text, resualt11 text, remark11 text, imgs11 text, resualt12 text, remark12 text, imgs12 text" +
				", resualt13 text, remark13 text, imgs13 text, resualt14 text, remark14 text, imgs14 text, resualt15 text, remark15 text, imgs15 text" +
				", resualt16 text, remark16 text, imgs16 text, resualt17 text, remark17 text, imgs17 text);";  
        Log.d(TAG, sql);
        db.execSQL(sql);
        //创建人员表
        sql = "create table if not exists check_user(user_id text primary key, user_name text, team_id text);";
        Log.d(TAG, sql);
        db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
	}
	
	/**
	 * 
	 * @param context
	 * @return
	 */
	public SQLiteDatabase getWriteableDataBase(Context context)
	{
		if(dataBaseHelper == null)
		{
			dataBaseHelper = new DataBaseHelper(context);
		}
		return dataBaseHelper.getWritableDatabase();
	}
	
	/**
	 * 
	 * @param context
	 * @return
	 */
	public SQLiteDatabase getReadableDataBase(Context context)
	{
		if(dataBaseHelper == null)
		{
			dataBaseHelper = new DataBaseHelper(context);
		}
		return dataBaseHelper.getReadableDatabase();
	}

}
